package org.yjc.controller;


import java.util.HashMap;
import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.DatagridUtils;
import org.yjc.custom.JournalUtils;
import org.yjc.custom.UserUtils;
import org.yjc.entity.Book;
import org.yjc.entity.College;
import org.yjc.entity.SysUser;
import org.yjc.form.BookForm;
import org.yjc.service.BookService;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value="/book")
public class BookController {
	@Autowired
	private BookService bookservice;
	@Autowired
	private UserUtils userutils;
	@Autowired
	private JournalUtils journalutils;
	private String path = "e:/java/book"; //存文件路径
	
	@RequestMapping(value="/bookmain")
	public String bookmain() {
		return "book/bookmain";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(DatagridUtils param,String bookname,String writer,String isbn) {
		Pageable pageable = param.buildPageable();
		Page<Book> page = null;
		//安全查询
		Specification<Book> spec = new Specification<Book>() {
			@Override
			public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(bookname)) {
					rules.add(criteriaBuilder.like(root.get("bookname"), "%"+bookname+"%"));
				}else if(StringUtils.hasText(writer)) {
					rules.add(criteriaBuilder.like(root.get("writer"), "%"+writer+"%"));
				}else if(StringUtils.hasText(isbn)) {
					rules.add(criteriaBuilder.equal(root.get("isbn"),isbn));
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		page = bookservice.findAll(spec,pageable);
		HashMap<String, Object> result = DatagridUtils.biuldDatagridResult(page);
		return result;
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(MultipartFile file,BookForm form) {
		Book book = new Book();
		SysUser sysuser = userutils.getUser();
		if(form.getId()!=null) {
			book = bookservice.findById(form.getId());
			book.setId(form.getId());
			BeanUtils.copyProperties(form, book);
			//录入日志
			journalutils.journal1("修改图书信息",sysuser.getName()+"进行了修改图书信息操作");
		}else {
			//录入日志
			journalutils.journal1("添加图书",sysuser.getName()+"添加了"+"("+form.getNumber()+"本"+form.getBookname()+")");
		}
		BeanUtils.copyProperties(form, book,"id");
		//判断编辑时是否选择了图片
		if(file.isEmpty()) {
			bookservice.save(book);
		}else {
			bookservice.save(file,book);
		}
		return "OK";
	}
	
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(Integer id, HttpServletRequest request, HttpServletResponse response) {
		bookservice.download(id, request, response);
		return null;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public String delete(String ids) {
		SysUser sysuser = userutils.getUser();
		String[] ids1 = ids.split(",");//去除“，” 将多个id存入数组中
		for(int i = 0;i<ids1.length;i++) {
			Book book = bookservice.findById(Integer.parseInt(ids1[i]));
			bookservice.deleteById(Integer.parseInt(ids1[i]));
			//日志
			journalutils.journal1("删除图书",sysuser.getName()+"删除了"+"("+book.getBookname()+")");
		}
		return "OK";
	}
	
	@RequestMapping(value="/review")
	public String review() {
		return "book/review";
	}
}
